package com.zhang.zhangapp.view.mine;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.user.Coupon;
import com.zhang.zhangapp.bean.user.CouponForJson;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;
import com.zhang.zhangapp.view.adapter.CashCouponAdapter;
import com.zhang.zhangapp.view.custom.HeadRefreshView;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/13.
 */

@ContentView(R.layout.activity_mine_coupon)
public class MineCashCouponActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    @ViewInject(R.id.coupon_listview)
    ListView listView;
    @ViewInject(R.id.refresh_layout)
    PullToRefreshLayout refreshLayout;

    List<Coupon> couponList = new ArrayList<>();

    private CashCouponAdapter cashCouponAdapter;

    private String shopCode = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shopCode=getIntent().getStringExtra("shopCode");
        x.view().inject(this);
        initView();
        getCashCoupons();
    }

    private void initView() {
        refreshLayout.setHeaderView(new HeadRefreshView(this));
        refreshLayout.setCanLoadMore(false);
        cashCouponAdapter = new CashCouponAdapter(this, couponList, shopCode);
        listView.setAdapter(cashCouponAdapter);
        titleText.setText("代金券");
        refreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                getCashCoupons();
            }

            @Override
            public void loadMore() {

            }
        });
    }


    @Event(type = View.OnClickListener.class, value = R.id.action_bar_left_image)
    private void back(View view) {
        this.finish();
    }

    private void getCashCoupons() {
        if (showLoadingDialog()) {
            Map<String, String> params = new HashMap<>();
            params.put("memberId", MApplication.getInstance().getUser().getMemberId());
            CommonHttp.commonHttp("hzg/member/queryTicket", params, new MCallback() {
                @Override
                public void onSuccess(String data) {
                    Gson gson = new Gson();
                    CouponForJson couponForJson = gson.fromJson(data, CouponForJson.class);
                    if (couponForJson != null && couponForJson.getDaijc() != null) {
                        couponList.addAll(couponForJson.getDaijc());
                        cashCouponAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onSuccess(String message, String code) {

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(Callback.CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    dismisionDalog();
                }
            });
        }
    }
}
